package ee.homework.third;

public class MinHeap {

	public void minHeapify(int[] heapArray, int i) {
		int l = 2 * i + 1;
		int r = 2 * i + 2;
		int smallest;
		if (l < heapArray.length && heapArray[l] < heapArray[i]) {
			smallest = l;
		} else {
			smallest = i;
		}
		if (r < heapArray.length && heapArray[r] < heapArray[smallest]) {
			smallest = r;
		}
		if (smallest != i) {
			int tmp = heapArray[i];
			heapArray[i] = heapArray[smallest];
			heapArray[smallest] = tmp;
			minHeapify(heapArray, smallest);
		}
	}

	public void buildMinHeap(int[] heapArray) {
		for (int i = heapArray.length / 2; i >= 0; i--) {
			minHeapify(heapArray, i);
		}
	}

	public void printList(int[] list) {
		for (int i = 0; i < list.length; i++) {
			System.out.print(list[i] + "  ");
		}
		System.out.println();
	}
}
